﻿using Aop.Api.FileControllers;
using System;
using System.Collections.Generic;
using System.DAL;
using System.Dto;
using System.IBLL;
using System.IDAL;
using System.Linq;
using System.Models.Models;
using System.Text;
using System.Threading.Tasks;

namespace System.BLL
{
    public class ParkingManager : IParkingManager
    {
        public async Task AddNewOneMonitoringInfo(GetMonitoringListDto dto)
        {
            using (IMonitoringService monitoringSvc = new MonitoringService())
            {
                Monitoring monitoring = new Monitoring()
                {
                    MonitorName = dto.monitorName,
                    MonitoringArea = dto.monitoringArea,
                    LargeScreenSrc = dto.largeScreenSrc,
                    Remark = dto.remark
                };
                await monitoringSvc.CreateAsync(monitoring);
            }
        }

        public async Task AddNewOneVideoDeposit(VideoDepositToSurveillanceVideoDto dto)
        {
            using (IVideoDepositService videoDepositSvc = new VideoDepositService())
            {
                VideoDeposit list = new VideoDeposit()
                {
                    DepositName = dto.depositName,
                    DepositSeat = dto.depositSeat,
                    Remark = dto.remark
                };
                FileHelper helper = new FileHelper();
                helper.CreatFileOrdeflt(dto.depositSeat);
                await videoDepositSvc.CreateAsync(list);
            }
        }

        public async Task AddOneParkingPlace(ParkingLotListDto dto)
        {
            using (IParkingLotService parkingLotSvc = new ParkingLotService())
            {
                ParkingLot parkingLot = new ParkingLot()
                {
                    ParkingSpaceNumber = dto.parkingSpaceNumber,
                    ParkinglLocation = dto.parkinglLocation,
                    Remark = dto.remark
                };
                await parkingLotSvc.CreateAsync(parkingLot);
            }
        }

        public async Task AddOneRentInfoByModel(GetRentInfoListDto dto)
        {
            using(IParkingDealService parkingDealSvc = new ParkingDealService())
            {
                ParkingDeal list = new ParkingDeal()
                {
                    Parkingid = dto.parkingId,
                    SalesAmount = double.Parse(dto.salesAmount),
                    SalesNature = dto.salesNature == "出租" ? false : true,
                    Remark = dto.remark,
                    SalesPeople = dto.salesPeople
                };
                await parkingDealSvc.CreateAsync(list);
            }
        }

        public async Task DelMonitoringInfoById(int id)
        {
            using (IMonitoringService monitoringSvc = new MonitoringService())
            {
                await monitoringSvc.RemoveAsync(id);
            }
        }

        public async Task DelOneParkingPlaceById(int id)
        {
            using (IParkingLotService parkingLotSvc = new ParkingLotService())
            {
                await parkingLotSvc.RemoveAsync(id);
            }
        }

        public async Task DelOneRentInfoById(int id)
        {
            using (IParkingDealService parkingDealSvc = new ParkingDealService())
            {
                await parkingDealSvc.RemoveAsync(id);
            }
        }

        public async Task DelOneSurveillanceVideoById(int id)
        {
            using(ISurveillanceVideoService surveillanceVideoSvc = new SurveillanceVideoService())
            {
                await surveillanceVideoSvc.RemoveAsync(id);
            }
        }

        public async Task EditOneMonitoringByDto(GetMonitoringListDto dto)
        {
            using (IMonitoringService monitoringSvc = new MonitoringService())
            {
                var list = monitoringSvc.GetOneByIdAsync(dto.Id);
                list.MonitorName = dto.monitorName;
                list.MonitoringArea = dto.monitoringArea;
                list.LargeScreenSrc = dto.largeScreenSrc;
                list.Remark = dto.remark;
                await monitoringSvc.EditAsync(list);
            }
        }

        public async Task EditOneParkingPlaceByDto(ParkingLotListDto dto)
        {
            using (IParkingLotService parkingLotSvc = new ParkingLotService())
            {
                var list = parkingLotSvc.GetOneByIdAsync(dto.Id);
                list.ParkingSpaceNumber = dto.parkingSpaceNumber;
                list.ParkinglLocation = dto.parkinglLocation;
                list.Remark = dto.remark;
                await parkingLotSvc.EditAsync(list);
            }
        }

        public async Task EditOneRentInfoByDto(GetRentInfoListDto dto)
        {
            using (IParkingDealService parkingDealSvc = new ParkingDealService())
            {
                var parkingDeal = parkingDealSvc.GetOneByIdAsync(dto.Id);
                parkingDeal.SalesNature = dto.salesNature == "出租" ? false : true;
                parkingDeal.SalesAmount = double.Parse(dto.salesAmount);
                parkingDeal.Remark = dto.remark;
                await parkingDealSvc.EditAsync(parkingDeal);
            }
        }

        public async Task EditOneSurveillanceVideoByDto(VideoDepositToSurveillanceVideoDto dto)
        {
            using(ISurveillanceVideoService surveillanceVideoSvc = new SurveillanceVideoService())
            {
                using(IVideoDepositService videoDepositSvc = new VideoDepositService())
                {
                    
                    var VideoData = surveillanceVideoSvc.GetOneByIdAsync(dto.Id);
                    var oldData = videoDepositSvc.GetOneByIdAsync(VideoData.DepositId);
                    var newData = videoDepositSvc.GetOneByIdAsync((int)dto.depositId);
                    string oldFilePath = oldData.DepositSeat + "\\" + VideoData.VideoName + ".mp4";
                    string newFilePath = newData.DepositSeat + "\\" + VideoData.VideoName + ".mp4";
                    FileHelper helper = new FileHelper();
                    helper.MoveFileOrdefilt(oldFilePath, newFilePath);

                    var list = surveillanceVideoSvc.GetOneByIdAsync(dto.Id);
                    if (!(dto.videoName == null || dto.videoName == ""))
                        list.VideoName = dto.videoName;
                    if (!(dto.remark == null || dto.remark == ""))
                        list.Remark = dto.remark;
                    if (dto.depositId != null)
                        list.DepositId = (int)dto.depositId;
                    await surveillanceVideoSvc.EditAsync(list);
                }

            }
        }

        public List<GetMonitoringListDto> getMonitoringListDtos(int limit, int page, out int datalet, string monitorName, string monitoringArea)
        {
            using (IMonitoringService monitoringSvc = new MonitoringService())
            {
                var data = monitoringSvc.GetAllAsync().Select(m => new GetMonitoringListDto()
                {
                    Id = m.Id,
                    monitorName = m.MonitorName,
                    monitoringArea = m.MonitoringArea,
                    largeScreenSrc = m.LargeScreenSrc,
                    remark = m.Remark,
                    createTime = m.CreateTime.ToString()
                }).ToList();

                if (!(monitorName == "" || monitorName == null))
                {
                    data = data.Where(m => m.monitorName.Contains(monitorName)).ToList();
                }
                if (!(monitoringArea == "" || monitoringArea == null))
                {
                    data = data.Where(m => m.monitoringArea.Contains(monitoringArea)).ToList();
                }

                datalet = data.Count();
                data = data.OrderBy(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;

            }
        }

        public GetMonitoringListDto getOneMonitoringById(int id)
        {
            using (IMonitoringService monitoringSvc = new MonitoringService())
            {
                var list = monitoringSvc.GetOneByIdAsync(id);
                GetMonitoringListDto data = new GetMonitoringListDto()
                {
                    Id = id,
                    monitorName = list.MonitorName,
                    monitoringArea = list.MonitoringArea,
                    largeScreenSrc = list.LargeScreenSrc,
                    remark = list.Remark
                };
                return data;
            }
        }

        public ParkingLotListDto GetOneParkingPlaceById(int id)
        {
            using (IParkingLotService parkingLotSvc = new ParkingLotService())
            {
                var list = parkingLotSvc.GetOneByIdAsync(id);
                ParkingLotListDto data = new ParkingLotListDto()
                {
                    Id = list.Id,
                    parkingSpaceNumber = list.ParkingSpaceNumber,
                    parkinglLocation = list.ParkinglLocation,
                    remark = list.Remark
                };
                return data;
            }
        }

        public GetRentInfoListDto GetOneRentInfoById(int id)
        {
            using (IParkingDealService parkingDealSvc = new ParkingDealService())
            {
                var list = parkingDealSvc.GetOneByIdAsync(id);
                GetRentInfoListDto data = new GetRentInfoListDto()
                {
                    Id = id,
                    parkingId = list.Parkingid,
                    salesNature = list.SalesNature ? "出售" : "出租",
                    salesAmount = list.SalesAmount.ToString(),
                    remark = list.Remark
                };
                using (IParkingLotService parkingLotSvc = new ParkingLotService())
                {
                    data.parkingSpaceNumber = parkingLotSvc.GetOneByIdAsync(data.parkingId).ParkingSpaceNumber;
                }
                return data;
            }
        }

        public List<ParkingLotListDto> GetparkingLotListDtos(int limit, int page, out int datalet, string Id, string parkinglLocation)
        {
            using (IParkingLotService parkingLotSvc = new ParkingLotService())
            {
                var data = parkingLotSvc.GetAllAsync().Select(m => new ParkingLotListDto()
                {
                    Id = m.Id,
                    parkingSpaceNumber = m.ParkingSpaceNumber,
                    parkinglLocation = m.ParkinglLocation,
                    createTime = m.CreateTime.ToString(),
                    remark = m.Remark
                }).ToList();


                if (!(Id == "" || Id == null))
                {
                    data = data.Where(m => m.Id == int.Parse(Id)).ToList();
                }
                if (!(parkinglLocation == "" || parkinglLocation == null))
                {
                    data = data.Where(m => m.parkinglLocation.Contains(parkinglLocation)).ToList();
                }

                datalet = data.Count();
                data = data.OrderBy(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;
            }
        }

        public List<ParkingLotListDto> GetParkingSpaceNumberLists()
        {
            using (IParkingLotService parkingLotSvc = new ParkingLotService())
            {
                var data = parkingLotSvc.GetAllAsync().Select(m => new ParkingLotListDto()
                {
                    Id = m.Id,
                    parkingSpaceNumber = m.ParkingSpaceNumber
                }).ToList();

                return data;
            }
        }

        public List<GetRentInfoListDto> getRentInfoListDtos(int limit, int page, out int datalet, string Id, string parkinglLocation, string salesNature)
        {
            using (IParkingDealService parkingDealSvc = new ParkingDealService())
            {
                var data = parkingDealSvc.GetAllAsync().Select(m => new GetRentInfoListDto()
                {
                    Id = m.Id,
                    parkingId = m.Parkingid,
                    salesNature = m.SalesNature ? "出售" : "出租",
                    salesAmount = m.SalesAmount.ToString(),
                    salesPeople = m.SalesPeople,
                    remark = m.Remark,
                    createTime = m.CreateTime.ToString()
                }).ToList();

                foreach(var list in data)
                {
                    using (IParkingLotService parkingLotSvc = new ParkingLotService()) 
                    {
                        var lotList = parkingLotSvc.GetOneByIdAsync(list.parkingId);
                        list.parkingSpaceNumber = lotList.ParkingSpaceNumber;
                        list.parkinglLocation = lotList.ParkinglLocation;
                        using(IParkingSaleService parkingSaleSvc = new ParkingSaleService())
                        {
                            var saleList = parkingSaleSvc.GetOneByIdAsync(list.parkingId);
                            if (saleList != null) 
                            {
                                list.salesState = "已完成";
                            }
                            list.salesState = "待交易";
                        }
                    }
                }
                if (!(Id == "" || Id == null))
                {
                    data = data.Where(m => m.Id==int.Parse(Id)).ToList();
                }
                if (!(parkinglLocation == "" || parkinglLocation == null))
                {
                    data = data.Where(m => m.parkinglLocation.Contains(parkinglLocation)).ToList();
                }
                if (!(salesNature == "" || salesNature == null))
                {
                    data = data.Where(m => m.salesNature.Contains(salesNature)).ToList();
                }

                datalet = data.Count();
                data = data.OrderBy(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;
            }
        }

        public List<VideoDepositToSurveillanceVideoDto> getVideoDepositLists()
        {
            using(IVideoDepositService videoDepositSvc = new VideoDepositService())
            {
                var data = videoDepositSvc.GetAllAsync().Select(m => new VideoDepositToSurveillanceVideoDto()
                {
                    depositId = m.Id,
                    depositName = m.DepositName,
                    depositSeat = m.DepositSeat
                }).ToList();

                return data;
            }
        }

        public VideoDepositToSurveillanceVideoDto GetVideoDepositToSurveillance(int id)
        {
            using(ISurveillanceVideoService surveillanceSvc = new SurveillanceVideoService())
            {
                var list = surveillanceSvc.GetOneByIdAsync(id);
                using(IVideoDepositService videoDepositSvc = new VideoDepositService())
                {
                    var Vlist = videoDepositSvc.GetOneByIdAsync(list.DepositId);
                    VideoDepositToSurveillanceVideoDto data = new VideoDepositToSurveillanceVideoDto()
                    {
                        Id = list.Id,
                        depositName = Vlist.DepositName,
                        depositSeat = Vlist.DepositSeat,
                        depositId = list.DepositId,
                        videoName = list.VideoName,
                        videoSize = list.VideoSize,
                        remark = list.Remark
                    };
                    return data;
                }
            }
        }

        public List<VideoDepositToSurveillanceVideoDto> GetVideoDepositToSurveillanceVideoLists(int limit, int page, out int datalet, string videoName, string depositId)
        {
            using (ISurveillanceVideoService surveillanceSvc = new SurveillanceVideoService())
            {
                var data = surveillanceSvc.GetAllAsync().Select(m => new VideoDepositToSurveillanceVideoDto()
                {
                    Id = m.Id,
                    videoName = m.VideoName,
                    depositId = m.DepositId,
                    videoSize = m.VideoSize,
                    createTime = m.CreateTime.ToString(),
                    remark = m.Remark
                }).ToList();

                foreach(var list in data)
                {
                    using(IVideoDepositService videoDepositSvc = new VideoDepositService())
                    {
                        var li = videoDepositSvc.GetOneByIdAsync((int)list.depositId);
                        list.depositName = li.DepositName;
                        list.depositSeat = li.DepositSeat;
                    }
                }
                if (!(videoName == "" || videoName == null))
                {
                    data = data.Where(m => m.videoName.Contains(videoName)).ToList();
                }
                if (!(depositId == "" || depositId == null))
                {
                    data = data.Where(m => m.depositId == int.Parse(depositId)).ToList();
                }

                datalet = data.Count();
                data = data.OrderBy(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;
            }
        }
    }
}
